/**  
 * @Title: FlowService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-03-23 15:09:58 
 */
package com.fsxgt.datagrid.executor.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson2.JSON;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class ServerService {

	@Autowired
    RestTemplate restTemplate;
	
	private static final Logger logger = LoggerFactory.getLogger(ServerService.class);
	@Value("${console.address}")
	private String address;
	@Value("${server.port}")
	private String port;
	
	@PostConstruct
	public void init() {
		
		register();
	}
	
	@Scheduled(cron = "0/10 * * * * ? ")
	public void register() {

		try {
			HttpHeaders headers = new HttpHeaders();
			headers.add("Content-Type", "application/json; charset=UTF-8");
			headers.setContentType(MediaType.APPLICATION_JSON);
			HashMap params = new HashMap();
			params.put("port", port);
			HttpEntity<HashMap<String, Object>> httpEntity = new HttpEntity(params, headers);
			Map result=restTemplate.postForObject("http://" + address + "/server/register", httpEntity, Map.class);
//			log.info("注册executor服务:"+JSON.toJSONString(result));
		} catch (Exception e) {
			logger.error("", e);
		}

	}
	
	
	
	

}
